#include "bn_core.h"
#include "bn_array.h"
#include "bn_bg_palettes.h"
#include "bn_blending_actions.h"
#include "bn_blending_fade_alpha.h"
#include "bn_fixed.h"
#include "bn_math.h"
#include "bn_regular_bg_ptr.h"
#include "bn_regular_bg_items_backdrop.h"
#include "bn_regular_bg_items_border.h"
#include "bn_regular_bg_items_foreground.h"
#include "bn_sprite_actions.h"
#include "bn_sprite_animate_actions.h"
#include <bn_sprite_double_size_mode.h>
#include "bn_sprite_items_logo.h"
#include "bn_sprite_items_logo2.h"
#include "bn_sprite_items_p1.h"
#include "bn_sprite_items_p2.h"
#include "bn_sprite_items_p3.h"
#include "bn_sprite_position_hbe_ptr.h"
#include "bn_sprite_ptr.h"

int main()
{
    bn::core::init();

    bn::bg_palettes::set_transparent_color(bn::color(30,30,30));

    bn::regular_bg_ptr b = bn::regular_bg_items::backdrop.create_bg(-136,176);
    bn::regular_bg_ptr f = bn::regular_bg_items::foreground.create_bg(-136,176);

    bn::fixed br = 1;

    bn::bg_palettes::set_brightness(br);
    int frame_counter = 0;
    int x1 = -136, x2 = -136, y1 = -10;
    int angle = 60, scale = 3, anim_bg_counter = 180, anim_bg_counter2 = -450, speed = 11;

    bool woof = false;

    bn::sprite_ptr s1 = bn::sprite_items::logo.create_sprite(-76, y1);
    bn::sprite_ptr s2 = bn::sprite_items::logo2.create_sprite(-12, y1);
    bn::sprite_ptr p = bn::sprite_items::p2.create_sprite(anim_bg_counter,0);
    bn::sprite_ptr p2 = bn::sprite_items::p3.create_sprite(anim_bg_counter2,0);

    bn::sprite_animate_action<6> p_anim = bn::create_sprite_animate_action_forever
                        (p, speed, bn::sprite_items::p2.tiles_item(), 0,1,2,3,4,5);
    bn::sprite_animate_action<6> p2_anim = bn::create_sprite_animate_action_forever
                        (p2, 7, bn::sprite_items::p3.tiles_item(), 0,1,2,3,4,5);

    p.set_vertical_scale(2);
    p.set_horizontal_scale(2);
    p.set_double_size_mode(bn::sprite_double_size_mode::ENABLED);
    p.set_blending_enabled(true);

    p2.set_vertical_scale(2);
    p2.set_horizontal_scale(2);
    p2.set_double_size_mode(bn::sprite_double_size_mode::ENABLED);
    p2.set_blending_enabled(true);

    bn::blending::set_transparency_alpha(0.5);

    s1.set_visible(false);
    s2.set_visible(false);

    bn::array<bn::fixed, 160> hd;

    for (int i = 0; i < 160; ++i)
    {
        hd[i] = -180 + 160 - i;
    }

    bn::sprite_position_hbe_ptr hp1 =
            bn::sprite_position_hbe_ptr::create_horizontal(s1, hd);
    bn::sprite_position_hbe_ptr hp2 =
            bn::sprite_position_hbe_ptr::create_horizontal(s2, hd);

    bn::regular_bg_ptr a = bn::regular_bg_items::border.create_bg(0,0);

    while(true)
    {
        bn::core::update();

        if (frame_counter < 2000)
        {
            ++frame_counter;

            if (frame_counter <= 90 && frame_counter % 10 == 0)
            {
                br -= 0.1;
                bn::bg_palettes::set_brightness(br);
            }
            if (frame_counter <= 768)
            {
                if (frame_counter % 3 == 0)
                    f.set_position(++x1, 176);
                if (frame_counter % 4 == 0)
                    b.set_position(++x2, 176);
            }
            if (frame_counter > 840)
            {
                a.set_visible(false);
                s1.set_visible(true);
                s2.set_visible(true);
                for(int index = 0; index < 80; ++index)
                {
                    hd[index] = hd[index] >= 0 ? 0: hd[index] + index%8+1;
                }

                s1.set_position(-76, y1);
                s2.set_position(-12, y1);
                s1.set_rotation_angle(bn::max(--angle, 0));
                s2.set_rotation_angle(bn::max(--angle, 0));
                scale-=0.05;
                s1.set_vertical_scale(bn::max(scale, 1));
                s2.set_vertical_scale(bn::max(scale, 1));

                if (frame_counter % 3 == 0 && y1 >= -42) --y1;

                hp1.reload_deltas_ref();
                hp2.reload_deltas_ref();
            }
        }
        p.set_position(bn::max(-170,--anim_bg_counter),0);
        p2.set_position(bn::min(370,anim_bg_counter2+=2),0);
        if (anim_bg_counter <= -210 && woof == false)
        {
            woof = true;
            anim_bg_counter = 160;
            speed = 8;
            p.set_position(anim_bg_counter,0);
            p_anim = bn::create_sprite_animate_action_forever
                    (p, speed, bn::sprite_items::p1.tiles_item(), 0,1,2,3,4,5);
        }
        p2_anim.update();
        p_anim.update();
    }
}
